﻿
using System;
using Calabonga.Xml.Exports;
using System.Linq;


namespace ABC.Business.Models
{
    public class ExcelModel
    {
        private readonly RbsCalcModel rbsCalcModel;

        public ExcelModel(RbsCalcModel rbsCalcModel)
        {
            this.rbsCalcModel = rbsCalcModel;
        }

        public string GetXlsKaskoResults(string resultId)
        {
            var wb = new Workbook();
            // properties
            wb.Properties.Author = "Азбука24";
            wb.Properties.Created = DateTime.Today;
            wb.Properties.LastAutor = "Азбука24";
            wb.Properties.Version = "01";
            
            // options sheets
            wb.ExcelWorkbook.ActiveSheet = 1;
            wb.ExcelWorkbook.DisplayInkNotes = false;
            wb.ExcelWorkbook.FirstVisibleSheet = 1;
            wb.ExcelWorkbook.ProtectStructure = false;
            wb.ExcelWorkbook.WindowHeight = 1024;
            wb.ExcelWorkbook.WindowTopX = 0;
            wb.ExcelWorkbook.WindowTopY = 0;
            wb.ExcelWorkbook.WindowWidth = 768;

            // create style s1 for header
            var s1 = new Style("s1");
            s1.Font.Bold = true;
            s1.Font.Italic = true;
            s1.Font.Size = 14;
            s1.Alignment.Horizontal = Horizontal.Center;
            s1.Alignment.Vertical = Vertical.Center;
            wb.AddStyle(s1);

            // First sheet
            var ws = new Worksheet("Расчет КАСКО");

            // adding headers
            ws.AddCell(0, 0, "Страховая компания", 0, mergeDown: 1, styleID: s1.UniqueName);
            ws.AddCell(0, 1, "КАСКО", 0, mergeAcross: 1, styleID: s1.UniqueName);
            ws.AddCell(0, 2, "Ущерб", 0, mergeAcross: 1, styleID: s1.UniqueName);
            ws.AddCell(1, 1, "Тариф", 2, s1.UniqueName);
            ws.AddCell(1, 2, "Премия", 0, s1.UniqueName);
            ws.AddCell(1, 3, "Тариф", 0, s1.UniqueName);
            ws.AddCell(1, 4, "Премия", 0, s1.UniqueName);
            ws.AddCell(0, 5, "Франшиза", 0, mergeDown: 1, styleID: s1.UniqueName);
            ws.AddCell(0, 6, "Сервисные программы", 0, mergeDown: 1, styleID: s1.UniqueName);
            ws.AddCell(0, 7, "Прочие условия", 0, mergeDown: 1, styleID: s1.UniqueName);
            
            var calc = rbsCalcModel.GetCalculationKasko(resultId);
            for (var i = 0; i < calc.KaskoResults.Count; i++)
            {
                ws.AddCell(i + 2, 0, calc.KaskoResults[i].CompanyName, 0);
                ws.AddCell(i + 2, 1, calc.KaskoResults[i].KaskoRate + "%", 0);
                ws.AddCell(i + 2, 2, calc.KaskoResults[i].KaskoValue + " руб.", 0);
                ws.AddCell(i + 2, 3, calc.KaskoResults[i].DamageRate + "%", 0);
                ws.AddCell(i + 2, 4, calc.KaskoResults[i].DamageValue + " руб.", 0);
                ws.AddCell(i + 2, 5, calc.KaskoResults[i].Deductible, 0);
                ws.AddCell(i + 2, 6, calc.KaskoResults[i].ServiceProgram, 0);
                ws.AddCell(i + 2, 7, calc.KaskoResults[i].AdditionalConditions, 0);
            }
            wb.AddWorksheet(ws);

            
            string workstring = wb.ExportToXML();
            //изменение ширини столбцов
            workstring = workstring.Replace("<Column ss:AutoFitWidth=\"0\" ss:Width=\"75\" />", "<Column ss:AutoFitWidth=\"0\" ss:Width=\"\" />");
            workstring = SetWidthColumn(workstring, "144");
            workstring = SetWidthColumn(workstring, "75");
            workstring = SetWidthColumn(workstring, "75");
            workstring = SetWidthColumn(workstring, "75");
            workstring = SetWidthColumn(workstring, "75");
            workstring = SetWidthColumn(workstring, "75");
            workstring = SetWidthColumn(workstring, "154");
            workstring = SetWidthColumn(workstring, "107");
            return workstring;
        }
        public string SetWidthColumn(string str, string width)
        {
            return str.Insert(str.IndexOf("<Column ss:AutoFitWidth=\"0\" ss:Width=\"\" />") + 38, width);
        }
    }
}
